﻿using BootstrapBlazor.Components;
using HelloApp.Server.Components;
using HelloApp.Server.Components.Pages;
using HelloApp.Server.Data;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using System;
using System.IO;
using System.Text;
using System.Text.Json;

var builder = WebApplication.CreateBuilder(args);

//日志
builder.Logging.ClearProviders();
builder.Logging.AddConsole();

// Add services to the container.
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
builder.Services.AddRazorComponents().AddInteractiveServerComponents();
//builder.Services.AddTableDemoDataService();
builder.Services.AddBootstrapBlazor();

builder.Services.AddSingleton(typeof(IDataService<>), typeof(CustomDataService<>));

// 增加 SignalR 服务数据传输大小限制配置
builder.Services.Configure<HubOptions>(option => option.MaximumReceiveMessageSize = null);
var app = builder.Build();
//System.Console.WriteLine(builder.Environment.ContentRootPath);

if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");
}

app.UseStaticFiles();
app.UseAntiforgery();
app.MapRazorComponents<App>().AddInteractiveServerRenderMode();

app.MapGet("api/materials", async ([FromServices] IDataService<Material> mats) => {
    var res = await mats.QueryAsync(new QueryPageOptions());
    return res.Items;
});

app.MapGet("api/config", ([FromServices]IWebHostEnvironment even) => {
    var path = Path.Combine(even.WebRootPath, "configs", "global.json");
    var config = JsonSerializer.Deserialize<MattressConfig>(File.ReadAllText(path));
    return config;
});

app.Run();
